package qdu.testsocket4;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class TestSocketClient {
    Socket s;
    BufferedWriter bw;
    BufferedReader br;
    void connetToServer(){
        //向服务端发起网络连接
        try {
            s = new Socket("192.168.47.99", 9999);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }
    public static void main(String[] args) {
        TestSocketClient tsc = new TestSocketClient();
        tsc.connetToServer();//发起连接请求
        //启动客户端发送消息线程
        tsc.new ClientMessageToServer().start();//(非静态内部类的对象创建)

        //启动客户端接收消息线程
        tsc.new ClientMessageReceiver().start();
        
    }
    class ClientMessageReceiver extends Thread{
        public void run(){
            try {
                if(s!=null && !s.isClosed()){
                    br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    //System.out.println("input stream ready....");
                }
                while(s!=null && !s.isClosed()){
                    //System.out.println("ready to receive....");
                    String message = br.readLine();
                    //System.out.println("an update....");
                    if(message != null && !"".equals(message.trim())){
                        System.out.println(message);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    class ClientMessageToServer extends Thread{
        public void run(){
            Scanner scanner = null;
            try {
                bw = new BufferedWriter(
                    new OutputStreamWriter(s.getOutputStream())
                );
                scanner = new Scanner(System.in);//System.in是进程对应的标准输入流，一般打开关闭一次 
                while(s!=null && !s.isClosed()){                                       
                    String message = scanner.nextLine();//接受键盘输入，阻塞式
                    //System.out.println("client msg inpection: " + message.trim());
                    bw.write(message.trim() + '\n');
                    bw.flush();
                    //System.out.println("client msg send....");                 
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                try {
                    if(s!=null && !s.isClosed()){
                        s.close();
                    }
                    if(bw !=null){
                        bw.close();
                    }
                    if(br != null)
                        br.close();
                    scanner.close();
                } catch (Exception e) { e.printStackTrace(); }
            } 
            
        }
    }
}
